package com.chris.libs.base.x.mvpp.recycler;

import android.content.Context;
import android.view.View;

import com.alibaba.android.vlayout.LayoutHelper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Create by Chris Chan
 * Create on 2023/4/1 8:13
 * Use for:
 * Explain:
 */
public class DefaultVLayoutAdapter<D, VH extends BaseVLayoutViewHolder<D>> extends BaseVLayoutAdapter<D, VH> {
    private int layoutId;
    private Class<VH> viewHolderClass;
    private LayoutHelper layoutHelper;

    public static <D, VH extends BaseVLayoutViewHolder<D>> DefaultVLayoutAdapterBuilder<D, VH> builder() {
        return DefaultVLayoutAdapterBuilder.newBuilder();
    }

    public DefaultVLayoutAdapter(String tag, Context context, List<D> dataList, OnItemClickListener onItemClickListener) {
        super(tag, context, dataList, onItemClickListener);
    }

    public static <D, VH extends BaseVLayoutViewHolder<D>> DefaultVLayoutAdapter<D, VH> create(String tag, Context context, int layoutId, Class<VH> viewHolderClass, LayoutHelper layoutHelper, List<D> dataList, OnItemClickListener onItemClickListener) {
        return new DefaultVLayoutAdapter<>(tag, context, layoutId, viewHolderClass, layoutHelper, dataList, onItemClickListener);
    }

    public DefaultVLayoutAdapter(String tag, Context context, int layoutId, Class<VH> viewHolderClass, LayoutHelper layoutHelper, List<D> dataList, OnItemClickListener onItemClickListener) {
        super(tag, context, dataList, onItemClickListener);
        this.layoutId = layoutId;
        this.viewHolderClass = viewHolderClass;
        this.layoutHelper = layoutHelper;
    }

    @Override
    protected int layoutId() {
        return this.layoutId;
    }

    @Override
    protected VH createViewHolder(View itemView) {
        Constructor<VH> constructor = null;

        try {
            //找到viewHolderClass的构建器 4个参数
            constructor = viewHolderClass.getDeclaredConstructor(String.class, Context.class, View.class, BaseVLayoutAdapter.OnItemClickListener.class);
            //执行构建器，创建对象
            return constructor.newInstance(tag, context, itemView, onItemClickListener);
        } catch (NoSuchMethodException e1) {
            try {
                //找到viewHolderClass的构建器 3个参数
                constructor = viewHolderClass.getDeclaredConstructor(Context.class, View.class, BaseVLayoutAdapter.OnItemClickListener.class);
                //执行构建器，创建对象
                return constructor.newInstance(context, itemView, onItemClickListener);
            } catch (NoSuchMethodException e2) {
                //如果有监听器的构造器没有找到，那就找没有监听器的
                try {
                    constructor = viewHolderClass.getDeclaredConstructor(Context.class, View.class);
                    //执行构建器，创建对象
                    return constructor.newInstance(context, itemView);
                } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException ex) {
                    //ex.printStackTrace();
                    throw new RuntimeException("没找到符合要求的构造器");
                }

            } catch (IllegalAccessException | InstantiationException | InvocationTargetException e3) {
                e3.printStackTrace();
            }
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException e4) {
            e4.printStackTrace();
        }

        return null;
    }

    @Override
    protected void refreshItemView(VH holder, D item) {
        holder.refreshView(item);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return this.layoutHelper;
    }


}
